﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.Practices.Unity;

namespace EasySite.Infrastructure
{
    public class UnityDependencyResolver : IDependencyResolver
    {        
        private readonly IUnityContainer _unityContainer;

        public UnityDependencyResolver(IUnityContainer unityContainer)
        {
            this._unityContainer = unityContainer;
        }

        #region IDependencyResolver Members

        public object GetService(Type serviceType)
        {
            try
            {
                return this._unityContainer.Resolve(serviceType);
            }
            catch
            {
                // Must return null
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            try
            {
                return this._unityContainer.ResolveAll(serviceType);
            }
            catch
            {
                // Must renturn empty list.
                return new List<object>();
            }
        }

        #endregion
    }
}
